/**
 * 作者: wgx
 * 日期: 2025-03-12 11:04:48
 * 备注: 物资争夺全服悬赏信息
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.CsGameFight.PBServerSnatchOffer;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class ServerSnatchOfferModel extends BaseModel implements Model {
    public static final String LISTKEY = "server_id.user_id";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // 数据库ID
    protected int serverId; // 服务器id
    protected long userId; // 被悬赏的玩家id
    protected String userName = ""; // 被悬赏的玩家名称
    protected long power; // 被悬赏的玩家战力
    protected String partner = ""; // 被悬赏的玩家的干员快照
    protected String icon = ""; // 被悬赏的玩家的icon
    protected int type; // 悬赏类型 (1普通悬赏2-高级悬赏)
    protected long userIdPublish; // 发布悬赏的玩家id
    protected long publishTime; // 发布时间
    protected String fighter = ""; // 已追击的玩家列表
    protected String fighterInfo = ""; // 追击详情
    protected int koNum; // 悬赏期间总的被击败干员数量
    protected long completeTime; // 结束时间
    protected int isSendReward; // 是否已经给发布悬赏的人发放奖励 (1-已发放)

    public void reset() {
    }

    protected String getKey() {
        return "ServerSnatchOffer." + serverId + "." + userId;
    }

    protected String getListKey() {
        return "ServerSnatchOffer." + serverId;
    }

    protected String getListItemKey() {
        return userId + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("server_snatch_offer"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, 0, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // 数据库ID
        obj.put("serverId", serverId); // 服务器id
        obj.put("userId", userId); // 被悬赏的玩家id
        obj.put("userName", userName); // 被悬赏的玩家名称
        obj.put("power", power); // 被悬赏的玩家战力
        obj.put("partner", partner); // 被悬赏的玩家的干员快照
        obj.put("icon", icon); // 被悬赏的玩家的icon
        obj.put("type", type); // 悬赏类型 (1普通悬赏2-高级悬赏)
        obj.put("userIdPublish", userIdPublish); // 发布悬赏的玩家id
        obj.put("publishTime", publishTime); // 发布时间
        obj.put("fighter", fighter); // 已追击的玩家列表
        obj.put("fighterInfo", fighterInfo); // 追击详情
        obj.put("koNum", koNum); // 悬赏期间总的被击败干员数量
        obj.put("completeTime", completeTime); // 结束时间
        obj.put("isSendReward", isSendReward); // 是否已经给发布悬赏的人发放奖励 (1-已发放)
        return obj;
    }

    public PBServerSnatchOffer.Builder takeInitBuilder() {
        PBServerSnatchOffer.Builder sb = PBServerSnatchOffer.newBuilder();
        if (userId != 0) {
            sb.setUserId(userId);
        }
        if (!userName.isEmpty()) {
            sb.setUserName(userName);
        }
        if (power != 0) {
            sb.setPower(power);
        }
        if (!partner.isEmpty()) {
            sb.setPartner(partner);
        }
        if (!icon.isEmpty()) {
            sb.setIcon(icon);
        }
        if (type != 0) {
            sb.setType(type);
        }
        if (userIdPublish != 0) {
            sb.setUserIdPublish(userIdPublish);
        }
        if (publishTime != 0) {
            sb.setPublishTime(publishTime);
        }
        if (this.fighter != null && this.fighter.length() > 0) {
            JsonArray list = new JsonArray(this.fighter);
            for(int i=0; i<list.size(); i++) {
                sb.addFighter(list.getLong(i));
            }
        }
        if (!fighterInfo.isEmpty()) {
            sb.setFighterInfo(fighterInfo);
        }
        return sb;
    }

    public PBServerSnatchOffer.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBServerSnatchOffer.Builder sb = PBServerSnatchOffer.newBuilder();
        if (isFieldUpdate("userId")) {
            sb.setUserId(userId);
        }
        if (isFieldUpdate("userName")) {
            sb.setUserName(userName);
        }
        if (isFieldUpdate("power")) {
            sb.setPower(power);
        }
        if (isFieldUpdate("partner")) {
            sb.setPartner(partner);
        }
        if (isFieldUpdate("icon")) {
            sb.setIcon(icon);
        }
        if (isFieldUpdate("type")) {
            sb.setType(type);
        }
        if (isFieldUpdate("userIdPublish")) {
            sb.setUserIdPublish(userIdPublish);
        }
        if (isFieldUpdate("publishTime")) {
            sb.setPublishTime(publishTime);
        }
        if (isFieldUpdate("fighter")) {
            if (this.fighter != null && this.fighter.length() > 0) {
                JsonArray list = new JsonArray(this.fighter);
                for(int i=0; i<list.size(); i++) {
                    sb.addFighter(list.getLong(i));
                }
                if (list.isEmpty()) {
                    sb.clearFighter();
                }
            } else {
                sb.clearFighter();
            }
        }
        if (isFieldUpdate("fighterInfo")) {
            sb.setFighterInfo(fighterInfo);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public int getServerId() {
        return serverId;
    }

    public void putServerId(int serverId) {
        if (this.serverId != serverId) {
            this.serverId = serverId;
            updateRedisHashField(getKey(), "serverId", this.serverId);
        }
    }

    public void addServerId(int value) {
        this.serverId += value;
        updateRedisHashField(getKey(), "serverId", this.serverId);
    }

    public long getUserId() {
        return userId;
    }

    public void putUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            updateRedisHashField(getKey(), "userId", this.userId);
        }
    }

    public void addUserId(long value) {
        this.userId += value;
        updateRedisHashField(getKey(), "userId", this.userId);
    }

    public String getUserName() {
        return userName;
    }

    public void putUserName(String userName) {
        if (!this.userName.equals(userName)) {
            this.userName = userName;
            updateRedisHashField(getKey(), "userName", this.userName);
        }
    }

    public long getPower() {
        return power;
    }

    public void putPower(long power) {
        if (this.power != power) {
            this.power = power;
            updateRedisHashField(getKey(), "power", this.power);
        }
    }

    public void addPower(long value) {
        this.power += value;
        updateRedisHashField(getKey(), "power", this.power);
    }

    public String getPartner() {
        return partner;
    }

    public void putPartner(String partner) {
        if (!this.partner.equals(partner)) {
            this.partner = partner;
            updateRedisHashField(getKey(), "partner", this.partner);
        }
    }

    public String getIcon() {
        return icon;
    }

    public void putIcon(String icon) {
        if (!this.icon.equals(icon)) {
            this.icon = icon;
            updateRedisHashField(getKey(), "icon", this.icon);
        }
    }

    public int getType() {
        return type;
    }

    public void putType(int type) {
        if (this.type != type) {
            this.type = type;
            updateRedisHashField(getKey(), "type", this.type);
        }
    }

    public void addType(int value) {
        this.type += value;
        updateRedisHashField(getKey(), "type", this.type);
    }

    public long getUserIdPublish() {
        return userIdPublish;
    }

    public void putUserIdPublish(long userIdPublish) {
        if (this.userIdPublish != userIdPublish) {
            this.userIdPublish = userIdPublish;
            updateRedisHashField(getKey(), "userIdPublish", this.userIdPublish);
        }
    }

    public void addUserIdPublish(long value) {
        this.userIdPublish += value;
        updateRedisHashField(getKey(), "userIdPublish", this.userIdPublish);
    }

    public long getPublishTime() {
        return publishTime;
    }

    public void putPublishTime(long publishTime) {
        if (this.publishTime != publishTime) {
            this.publishTime = publishTime;
            updateRedisHashField(getKey(), "publishTime", this.publishTime);
        }
    }

    public void addPublishTime(long value) {
        this.publishTime += value;
        updateRedisHashField(getKey(), "publishTime", this.publishTime);
    }

    public JsonArray getFighterArray() {
        if (this.fighter != null && this.fighter.length() > 0) {
            return new JsonArray(this.fighter);
        } else {
            return new JsonArray();
        }
    }

    public String getFighter() {
        return fighter;
    }

    public void putFighter(String fighter) {
        if (!this.fighter.equals(fighter)) {
            this.fighter = fighter;
            updateRedisHashField(getKey(), "fighter", this.fighter);
        }
    }

    public String getFighterInfo() {
        return fighterInfo;
    }

    public void putFighterInfo(String fighterInfo) {
        if (!this.fighterInfo.equals(fighterInfo)) {
            this.fighterInfo = fighterInfo;
            updateRedisHashField(getKey(), "fighterInfo", this.fighterInfo);
        }
    }

    public int getKoNum() {
        return koNum;
    }

    public void putKoNum(int koNum) {
        if (this.koNum != koNum) {
            this.koNum = koNum;
            updateRedisHashField(getKey(), "koNum", this.koNum);
        }
    }

    public void addKoNum(int value) {
        this.koNum += value;
        updateRedisHashField(getKey(), "koNum", this.koNum);
    }

    public long getCompleteTime() {
        return completeTime;
    }

    public void putCompleteTime(long completeTime) {
        if (this.completeTime != completeTime) {
            this.completeTime = completeTime;
            updateRedisHashField(getKey(), "completeTime", this.completeTime);
        }
    }

    public void addCompleteTime(long value) {
        this.completeTime += value;
        updateRedisHashField(getKey(), "completeTime", this.completeTime);
    }

    public int getIsSendReward() {
        return isSendReward;
    }

    public void putIsSendReward(int isSendReward) {
        if (this.isSendReward != isSendReward) {
            this.isSendReward = isSendReward;
            updateRedisHashField(getKey(), "isSendReward", this.isSendReward);
        }
    }

    public void addIsSendReward(int value) {
        this.isSendReward += value;
        updateRedisHashField(getKey(), "isSendReward", this.isSendReward);
    }

}